<template>
  <div class="signup-page mx-auto p-3 w-330">
    <h5 class="my-4 text-center">注册者也账户</h5>
    <validate-form @form-submit="onFormSubmit" ref="loginForm">
      <div class="mb-3">
        <label class="form-label">邮箱地址</label>
        <validate-input
          type="text"
          placeholder="请输入邮箱地址"
          :rules="emailRules"
          v-model="email"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">昵称</label>
        <validate-input
          type="text"
          placeholder="请输入昵称"
          :rules="nameRules"
          v-model="nickName"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">密码</label>
        <validate-input
          type="password"
          placeholder="请输入密码"
          :rules="passwordRules"
          v-model="password"
        />
      </div>
      <div class="mb-3">
        <label class="form-label">重复密码</label>
        <validate-input
          type="password"
          placeholder="请再次密码"
          :rules="repeatPasswordRules"
          v-model="repeatPassword"
        />
      </div>
      <template #submit>
        <button type="submit" class="btn btn-primary">注册新用户</button>
      </template>
    </validate-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, toRefs } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import ValidateInput, { RulesProp } from '../components/ValidateInput.vue'
import ValidateForm from '../components/ValidateForm.vue'
import createMessage from '../components/createMessage'

export default defineComponent({
  name: 'Signup',
  components: {
    ValidateInput,
    ValidateForm
  },
  setup () {
    const formData = reactive({
      email: '',
      nickName: '',
      password: '',
      repeatPassword: ''
    })
    const loginForm = ref()
    const router = useRouter()
    const emailRules: RulesProp = [
      { type: 'required', message: '电子邮箱地址不能为空' },
      { type: 'email', message: '请输入正确的电子邮箱格式' },
      {
        type: 'range',
        min: { length: 6, message: '邮箱的长度为6-20位, 不能含有空格！' },
        max: { length: 20, message: '邮箱的长度为6-20位, 不能含有空格！' }
      }
    ]
    const nameRules: RulesProp = [
      { type: 'required', message: '昵称不能为空' },
      { type: 'range', min: { length: 2, message: '昵称必须大于两个字符，不能含有空格！' } }
    ]
    const passwordRules: RulesProp = [
      { type: 'required', message: '密码不能为空！' },
      { type: 'range', min: { length: 6, message: '密码至少包含6位，不能含有空格！' } }
    ]
    const repeatPasswordRules: RulesProp = [
      { type: 'required', message: '重复密码不能为空！' },
      {
        type: 'custom',
        validator: () => {
          return formData.password === formData.repeatPassword
        },
        message: '两次输入的密码不相同！'
      }
    ]
    const onFormSubmit = (result: boolean) => {
      if (result) {
        const payload = {
          email: formData.email,
          password: formData.password,
          nickName: formData.nickName
        }
        axios.post('/users/', payload).then(() => {
          createMessage('注册成功 正在跳转登录页面', 'success')
          setTimeout(() => {
            router.push('/login')
          }, 2000)
        }).catch(() => {
          loginForm.value.clearInputs()
        })
      } else {
        // 调用form表单返回的函数清空输入框
        loginForm.value.clearInputs()
      }
    }
    // toRefs把reactive对象中的每一项变成响应式的，返回时可以展开
    const formDataRef = toRefs(formData)
    return {
      emailRules,
      nameRules,
      passwordRules,
      repeatPasswordRules,
      onFormSubmit,
      ...formDataRef,
      loginForm
    }
  }
})
</script>

<style>
.w-330 {
  max-width: 330px;
}
</style>
